package com.ruoyi.finance.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.finance.domain.AccountUser;
import com.ruoyi.finance.mapper.AccountUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.TAccountSetsMapper;
import com.ruoyi.finance.domain.TAccountSets;
import com.ruoyi.finance.service.ITAccountSetsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class TAccountSetsServiceImpl implements ITAccountSetsService 
{
    @Autowired
    private TAccountSetsMapper tAccountSetsMapper;

    @Autowired
    AccountUserMapper accountUserMapper;

    /**
     * 查询账套
     * 
     * @param id 账套主键
     * @return 账套
     */
    @Override
    public TAccountSets selectTAccountSetsById(Long id)
    {
        return tAccountSetsMapper.selectTAccountSetsById(id);
    }

    /**
     * 查询账套列表
     * 
     * @param tAccountSets 账套
     * @return 账套
     */
    @Override
    public List<TAccountSets> selectTAccountSetsList(TAccountSets tAccountSets)
    {
        return tAccountSetsMapper.selectTAccountSetsList(tAccountSets);
    }

    /**
     * 新增账套
     * 
     * @param tAccountSets 账套
     * @return 结果
     */
    @Override
    public int insertTAccountSets(TAccountSets tAccountSets)
    {
        List<TAccountSets> tAccountSets1 = tAccountSetsMapper.selectList(new QueryWrapper<TAccountSets>().lambda().eq(TAccountSets::getName, tAccountSets.getName()));
        if (tAccountSets1.size() > 0) {
            throw new RuntimeException("账套名称已存在");
        }
        return tAccountSetsMapper.insert(tAccountSets);
    }

    /**
     * 修改账套
     * 
     * @param tAccountSets 账套
     * @return 结果
     */
    @Override
    public int updateTAccountSets(TAccountSets tAccountSets)
    {
        return tAccountSetsMapper.updateById(tAccountSets);
    }

    /**
     * 批量删除账套
     * 
     * @param ids 需要删除的账套主键
     * @return 结果
     */
    @Override
    public int deleteTAccountSetsByIds(Long[] ids)
    {
        return tAccountSetsMapper.deleteTAccountSetsByIds(ids);
    }

    /**
     * 删除账套信息
     * 
     * @param id 账套主键
     * @return 结果
     */
    @Override
    public int deleteTAccountSetsById(Long id)
    {
        return tAccountSetsMapper.deleteTAccountSetsById(id);
    }

    @Override
    public List<TAccountSets> listAccountSetAll() {
        Long userId = SecurityUtils.getUserId();
        return tAccountSetsMapper.listAccountSetAll(userId);
    }

    @Override
    @Transactional
    public R listAccountSetAllById(Long id) {
        Long userId = SecurityUtils.getUserId();
        //如果有登录用户的话，判断当前账套已经是默认账套了，不作操作
        LambdaQueryWrapper<AccountUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountUser::getUserId, userId);
        List<AccountUser> accountUsers = accountUserMapper.selectList(queryWrapper);
        if(accountUsers!=null && accountUsers.size()>0){
            AccountUser accountUser = accountUsers.get(0);
            if(accountUser.getAccountSetsId().equals(id)){
                throw new RuntimeException("当前账套已经是默认账套，请勿重复设置");
            }
            accountUserMapper.delete(queryWrapper);
        }

        AccountUser accountUser = new AccountUser();
        accountUser.setAccountSetsId(id);
        accountUser.setUserId(SecurityUtils.getUserId());

        accountUserMapper.insert(accountUser);


        return R.ok();
    }

}
